#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define ls num << 1
#define rs ls | 1
#define li ls, l, mid
#define ri rs, mid + 1, r
#define debug cerr << "\33[33m[" << __LINE__ << "]\33[m "
#define SZ(x) ((int) x.size() - 1)
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); ++i)
#define DF(i, x, y) for (int i = (x); i >= (y); --i)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> T& chkmax(T& x, T y) { return x = max(x, y); }
template <typename T> T& chkmin(T& x, T y) { return x = min(x, y); }
template <typename T> T& read(T& x) {
    x = 0; int f = 1; char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = - 1;
    for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x *= f;
}
const int N = 1.5e5 + 10, inf = 1e9;
int n, m, aa[N], a[N], c[N], k, ans[N * 2], pos1[N], pos2[N], qp[N * 2];//, pp[N];
// int w[25], tot;
vector <int> v[25];
void predfs(int dep, int num, int l, int r) {
    if (l == r) {
        qp[num] = l - 1;
        ans[num] = a[l];
        return;
    }
    predfs(dep - 1, li), predfs(dep - 1, ri);
    int id = l >> dep;
    if (!v[dep][id]) {
        if (ans[ls] >= dep) ans[num] = ans[ls];
        else ans[num] = ans[rs];
        if (ans[ls] >= dep) qp[num] = qp[ls];
        else qp[num] = qp[rs];
    } else {
        if (ans[rs] >= dep) ans[num] = ans[rs];
        else ans[num] = ans[ls];
        qp[num] = qp[rs];
    }
}
// int query(int dep, int num, int l, int r) {
//     if (!dep) {
//         // if (a[l] < x) return l;
//         return l - 1;
//     }
//     int id = l >> dep;
//     if (!v[dep][id]) {
//         if (ans[ls] >= dep) {
//             // assert(ans[ls] >= x);
//             return query(dep - 1, li);
//         }
//         return query(dep - 1, ri);
//     } else {
//         return query(dep - 1, ri);
//         // if (ans[rs] >= dep) {
//         //     assert(ans[rs] >= x);
//         // }
//     }
// }
void dfs(int dep, int num, int l, int r, int o, int lim, int lim2, int mn) {
    if (!dep) {
        if (a[l] < mn) pos1[l] = 0;
        else {
            pos1[l] = lim;
            // int wl = 0, wr = tot + 1;
            // while (wl + 1 < wr) {
            //     int wmid = (wl + wr) >> 1;
            //     if (w[wmid] > a[l]) wl = wmid;
            //     else wr = wmid;
            // }
            // if (wl) chkmin(pos1[l], (1 << (w[wl] - 1)));
            int t = (o >> (a[l] + 1));
            if (t) {
                int nt = t & - t;
                // if ((1 << (w[wl] - 1)) != (nt << a[l])) {
                //     debug << o << " " << a[l] << " " << tot << endl;
                //     F(i, 1, tot) cout << w[i] << " "; debug << endl;
                //     debug << (nt << a[l]) << " " << ((1 << (w[wl] - 1))) << endl;
                // }
                // assert(((1 << (w[wl] - 1))) == (nt << a[l]));
                chkmin(pos1[l], nt << a[l]);
            }
        }
        pos2[l] = lim2;
        return;
    }
    int id = l >> dep, llim = lim, rlim = lim, llim2 = lim2, rlim2 = lim2, oo = o;
    if (!v[dep][id]) {
        // w[++tot] = dep;
        oo ^= 1 << dep;
    } else {
        if (ans[rs] >= dep) {
            chkmin(llim, qp[rs]);
        }
    }
    dfs(dep - 1, li, oo, llim, llim2, mn);
    int mmn = mn;
    // if (!v[dep][id]) {
    //     tot--;
    //     // oo ^= 1 << dep;
    // }
    if (!v[dep][id]) {
        if (ans[ls] >= dep) {
            rlim = 0;
            chkmin(rlim2, qp[ls]);
        }
    } else {
        chkmax(mmn, dep);
    }
    dfs(dep - 1, ri, o, rlim, rlim2, mmn);
}
ll sum[N];
int g[N];
signed main() {
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    read(n), read(m);
    F(i, 1, n) {
        // int w = __lg(i - 1);
        read(aa[i]);
    }
    F(i, 1, m) {
        read(c[i]);
    }
    while ((1 << k) < n) k++;
    F(i, 1, 1 << k) {
        // pp[i] = 
        if (i == 1) g[i] = 1;
        else {
            // int w = __lg(i - 1);
            g[i] = (1 << __lg(i - 1)) + 1;
        }
    }
    F(i, 1, k) {
        v[i].resize(1 << (k - i));
        F(j, 0, (1 << (k - i)) - 1) {
            char c;
            do {
                c = getchar();
            } while (!isdigit(c));
            v[i][j] = c == '1';
        }
    }
    int t; read(t);
    while (t--) {
        int x[4];
        F(i, 0, 3) read(x[i]);
        F(i, 1, n) {
            a[i] = aa[i] ^ x[i % 4];
            chkmin(a[i], k);
            // cout << a[i] << " ";
        }
        // debug << endl;
        predfs(k, 1, 1, 1 << k);
        dfs(k, 1, 1, 1 << k, 0, n, n, 0);
        F(i, 1, n) sum[i] = 0;
        F(i, 1, (1 << k)) {
            if (pos1[i] >= i) {
                sum[i] += i;
                sum[pos1[i] + 1] -= i;
            }
            chkmin(pos2[i], i - 1);
            if (pos2[i] >= g[i]) {
                sum[g[i]] += i;
                sum[pos2[i] + 1] -= i;
            }
            // debug << i << " " << pos1[i] << " " << pos2[i] << " " << g[i] << endl;
        }
        F(i, 1, n) sum[i] += sum[i - 1];
        // F(i, 1, n) {
        //     debug << i << " " << sum[i] << endl;
        // }
        ll ans = 0;
        F(i, 1, m) {
            ans ^= i * sum[c[i]];
        }
        cout << ans << '\n';
    }
    return 0;
}